#![feature(libc)]
extern crate libc;

use libc::c_char;
use std::ffi::CString;

#[no_mangle]
pub extern fn hello_world() {
    println!("hello from rust");
}

//https://thefullsnack.com/en/string-ffi-rust.html
#[no_mangle]
pub extern fn return_str(name: *mut c_char) -> *const c_char {
    // let name = unsafe { CString::from_raw(name).to_str().unwrap().to_owned() };
    // println!("{}", name);
    let name = "chen";
    let s = format!("Hello, {}", name);
    let c_str = CString::new(s).unwrap();
    let r: *const c_char = c_str.as_ptr() as *const c_char;
    std::mem::forget(c_str);
    r
}

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }
}


